package cn.com.wangluotongxin.merchants.service.impl;

import cn.com.wangluotongxin.common.constants.CommonConstant;
import cn.com.wangluotongxin.common.exceptions.BadResponseException;
import cn.com.wangluotongxin.common.pojo.TenantUser;
import cn.com.wangluotongxin.merchants.dto.GoodsColorDto;
import cn.com.wangluotongxin.merchants.form.GoodsColorForm;
import cn.com.wangluotongxin.merchants.model.MerchantsGoodsColor;
import cn.com.wangluotongxin.merchants.mapper.MerchantsGoodsColorMapper;
import cn.com.wangluotongxin.merchants.model.constants.YesNo;
import cn.com.wangluotongxin.merchants.service.IMerchantsGoodsColorService;
import cn.com.wangluotongxin.merchants.vo.GoodsColorVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商户管理-商品管理-颜色 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2024-06-20
 */
@Service
public class MerchantsGoodsColorServiceImpl extends ServiceImpl<MerchantsGoodsColorMapper, MerchantsGoodsColor> implements IMerchantsGoodsColorService {

    @Override
    public IPage<GoodsColorVo> getPage(GoodsColorDto dto) {
        Page page = Page.of(dto.getPageNumber(), dto.getPageSize());
        return baseMapper.getPage(page, dto);
    }

    @Override
    public List<GoodsColorVo> getList(String colorName) {
        return baseMapper.getList(colorName);
    }

    @Override
    public void add(GoodsColorForm form, TenantUser tenantUser) {
        if(this.count(new LambdaQueryWrapper<MerchantsGoodsColor>()
                .eq(MerchantsGoodsColor::getColorName, form.getColorName())
                .ne(MerchantsGoodsColor::getIsDeleted, CommonConstant.ONE)) > 0) {
            throw new BadResponseException("颜色【"+ form.getColorName() +"】 已存在");
        }

        MerchantsGoodsColor goodsColor = new MerchantsGoodsColor();
        BeanUtils.copyProperties(form, goodsColor);
        goodsColor.setCreateUser(tenantUser.getId());
        goodsColor.setCreateName(tenantUser.getUsername());
        this.save(goodsColor);
    }

    @Override
    public void delete(Integer id) {
        this.update(new LambdaUpdateWrapper<MerchantsGoodsColor>()
                .set(MerchantsGoodsColor::getIsDeleted, YesNo.YES.getCode())
                .eq(MerchantsGoodsColor::getId, id));
    }

    @Override
    public void enabled(Integer id, Integer isEnabled) {
        this.update(new LambdaUpdateWrapper<MerchantsGoodsColor>()
                .set(MerchantsGoodsColor::getIsEnabled, isEnabled)
                .eq(MerchantsGoodsColor::getId, id));
    }
}
